﻿Imports System.ComponentModel
Imports System.Data.SqlClient
Imports System.IO
Public Class frmClb
    Dim context As New QLDBDataContext
    Dim clb As New dsCLB
    Dim hinh As New LayHinh
    Dim lt As New List(Of dsCLB)
    Dim path As String = Directory.GetCurrentDirectory
    Private Sub Reload()
        pic.Image = Nothing
        txtTenDoi.Text = String.Empty
        cboHLV.SelectedIndex = 0
        cboSVD.SelectedIndex = 0
    End Sub

    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Cursor = Cursors.AppStarting
        Reload()
        Me.Close()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub frmClb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Cursor = Cursors.AppStarting
        hlCLB.HelpNamespace = path + "\hlp\index.html#DoiBong"
        Try
            Dim listSVD As New BindingList(Of SANVD)
            listSVD = context.SANVDs.GetNewBindingList()
            cboSVD.DataSource = listSVD
            cboSVD.DisplayMember = "TenSan"
            cboSVD.ValueMember = "MaSan"

            Dim listHLV As New BindingList(Of HUANLUYENVIEN)
            listHLV = context.HUANLUYENVIENs.GetNewBindingList()
            cboHLV.DataSource = listHLV
            cboHLV.DisplayMember = "TenHLV"
            cboHLV.ValueMember = "MaHLV"

            If gridCLB.DataSource Is Nothing Then
                GridViewCLB()
            End If
            'If clb Is Nothing Then
            '    gridCLB.DataSource = clb.LayDoiBong
            'End If
            gridCLB.DataSource = clb.LayDoiBong
            If gridCLB.RowCount <> 20 Then
                Dim doi = 20 - gridCLB.RowCount
                MessageBox.Show("Số Đội Bóng Chưa Đủ, Bạn Cần Thêm " & doi & " Đội!")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
       
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub GridViewCLB()
        With gridCLB
            .ColumnCount = 5
            .Columns(0).DataPropertyName = "MaCLB"
            .Columns(0).Width = 70

            .Columns(1).DataPropertyName = "IMG"

            .Columns(2).DataPropertyName = "TenCLB"

            .Columns(3).DataPropertyName = "TenSan"

            .Columns(4).DataPropertyName = "HLV"

            
        End With
    End Sub

    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        Me.Cursor = Cursors.AppStarting

        If pic.Image Is Nothing Then
            MessageBox.Show("Chưa Chọn Dữ Liệu Để Xóa")
            Return
        ElseIf MessageBox.Show("Bạn Có Chắc Muốn Xóa Đội Bóng Đã Chọn?") = Windows.Forms.DialogResult.OK Then

            Dim todel As dsCLB = gridCLB.CurrentRow.DataBoundItem()
            Dim DelCLB As CAULACBO
            DelCLB = context.CAULACBOs.Single(Function(p) p.MaCLB = todel.MaCLB)

            context.CAULACBOs.DeleteOnSubmit(DelCLB)
            context.SubmitChanges()
            gridCLB.DataSource = clb.LayDoiBong
            Reload()
        Else
            Return
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub CapNhat()
        Dim sua As dsCLB = gridCLB.CurrentRow.DataBoundItem()
        Dim _clb As New CAULACBO
        _clb = context.CAULACBOs.Single(Function(p) p.MaCLB = sua.MaCLB)
        txtTenDoi.Text = sua.TenCLB
        pic.Image = sua.IMG
        cboHLV.SelectedValue = _clb.MaHLV
        cboSVD.SelectedValue = _clb.MaSan
    End Sub

    Private Sub gridCLB_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles gridCLB.CellContentDoubleClick
        CapNhat()
    End Sub

    Private Sub btTim_Click(sender As Object, e As EventArgs) Handles btTim.Click
        Me.Cursor = Cursors.AppStarting
        lt = clb.TimKiem(txtTenDoi.Text, cboSVD.SelectedValue, cboHLV.SelectedValue)
        If lt.Count = 0 Then
            MessageBox.Show("Thông Tin Bạn Muốn Tìm Không Tồn Tại.")
        Else

            gridCLB.DataSource = lt
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnChinhSua_Click(sender As Object, e As EventArgs) Handles btnChinhSua.Click
        Me.Cursor = Cursors.AppStarting
        If pic.Image Is Nothing Then
            MessageBox.Show("Chưa Chọn Dữ Liệu Để Sửa")
            Return
        ElseIf OpenFileDialog1.FileName = "openfile" Then

            Dim sua As dsCLB = gridCLB.CurrentRow.DataBoundItem()
            Dim _CLB As CAULACBO
            _CLB = context.CAULACBOs.Single(Function(p) p.MaCLB = sua.MaCLB)

            _CLB.TenCLB = txtTenDoi.Text
            _CLB.MaSan = cboSVD.SelectedValue
            _CLB.MaHLV = cboHLV.SelectedValue

            context.SubmitChanges()

            gridCLB.DataSource = clb.LayDoiBong()
            txtTenDoi.Text = String.Empty
            MessageBox.Show("Sửa thành công")
        Else
            Dim sua As dsCLB = gridCLB.CurrentRow.DataBoundItem()
            Dim _CLB As CAULACBO
            _CLB = context.CAULACBOs.Single(Function(p) p.MaCLB = sua.MaCLB)

            _CLB.TenCLB = txtTenDoi.Text
            _CLB.MaSan = cboSVD.SelectedValue
            _CLB.MaHLV = cboHLV.SelectedValue
            _CLB.LOGO = hinh.LuuHinh(OpenFileDialog1.FileName, "caulacbo\")
            context.SubmitChanges()
            gridCLB.DataSource = clb.LayDoiBong()
            Reload()
            MessageBox.Show("Sửa thành công")
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub gridCLB_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles gridCLB.CellContentClick
        Me.Cursor = Cursors.AppStarting
        CapNhat()
    End Sub

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        Me.Cursor = Cursors.AppStarting
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            pic.Image = hinh.ResizImg(OpenFileDialog1.FileName, pic.Width, pic.Height)
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        Me.Cursor = Cursors.AppStarting
        If gridCLB.RowCount = 20 Then
            MessageBox.Show("Danh Sách Đã Đủ")
        Else
            themCLB.ShowDialog()
        End If
        Me.Cursor = Cursors.Default
    End Sub
End Class